/**
 * Created by chen on 18/6/12.
 */
var promotion_barcodes = ['ITEM00001','ITEM00006'];//优惠促销barcode 买二送一

window.onload = function(){
  getCartListNum();
  var arr_cart_list = JSON.parse(localStorage.getItem("arr_cart_list"))||[];
  var tpl = document.getElementById('template_pay_info').innerHTML;
  var inner_tpl_string = template(tpl, {list:arr_cart_list});
  document.getElementById('pay_list_info').innerHTML = inner_tpl_string;


  //用moment获取时间
  var str_show_time = moment().format("YYYY年MM月DD日 HH:mm:ss");
  $("#show_time").text(str_show_time);

  //求优惠商品展示
  var arr_promotions = [];
  var total = 0;
  var save = 0;
  for (var i = 0; i < arr_cart_list.length; i ++)
  {
    var cart_item = arr_cart_list[i];
    var find_promotion = _.find(promotion_barcodes,function(barcode)
    {
      return barcode == cart_item.barcode;
    });
    if (find_promotion != undefined && cart_item.count >= 3)
    {
      var promotion_num = Math.floor(cart_item.count/3);
      var promotion_info = {type:cart_item.type,name:cart_item.name,count:promotion_num};
      arr_promotions.push(promotion_info);
      save += cart_item.price * promotion_num;
    }
    total += cart_item.price * cart_item.count;
  }


var promotion_tpl = document.getElementById('template_gift_info').innerHTML;
var promotion_tpl_string = template(promotion_tpl, {list:arr_promotions});
document.getElementById('gift_list_info').innerHTML = promotion_tpl_string;

  $("#total").text(total + '元');
  $("#save").text(save + '元');
}
function confirm(){
  localStorage.clear();
  window.location.href = "shopping_list.html";
}